package com.example.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class DisplayMessageActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String key = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        key = key.trim();

        // We search the key in the properties file
        String message = "Key not found...";
        
        Log.v("DisplayMessageActivity", "The selected key is: " + key);
        //http post
        InputStream is = null;
        BasicNameValuePair nvp = new BasicNameValuePair("key", key);
        ArrayList<BasicNameValuePair> nvpl = new ArrayList<BasicNameValuePair>();
        nvpl.add(nvp);
        try {
        	HttpClient httpclient = new DefaultHttpClient();
        	HttpPost httppost = new HttpPost("http://192.168.1.109/index.php");
        	httppost.setEntity(new UrlEncodedFormEntity(nvpl));
        	HttpResponse response = httpclient.execute(httppost);
        	HttpEntity entity = response.getEntity();
        	is = entity.getContent();
        	  
        }catch(Exception e){
        	Log.e("DisplayMessageActivity", "Error in http connection"+e.toString());
        }

        //convert response to string
        StringBuilder sb=null;
      		try{
      			BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
      			sb = new StringBuilder();
      			sb.append(reader.readLine() + "\n");
      			String line="0";
      	     
      			while ((line = reader.readLine()) != null) {
      				sb.append(line + "\n");
      			}
      			
      			is.close();
      			message=sb.toString();
      			Log.v("DisplayMessageActivity", "The returned message is: " + message);
      			
      		}catch(Exception e){
      			Log.e("log_tag", "Error converting result "+e.toString());
      		}
        
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_display_message, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
